package com.mt.grand.singleton.register;

import com.mt.grand.singleton.reflect.ReflectSingleton;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RegistSingleton {
    public static Map<String,RegistSingleton> registSingletonMap = new ConcurrentHashMap<>();

    protected RegistSingleton(){}

    public static RegistSingleton getInstance(String name){
        if(name == null){
            name = RegistSingleton.class.getName();
        }
        if(registSingletonMap.get(name)==null){
            synchronized (RegistSingleton.class){
                if(registSingletonMap.get(name)==null){
                    try {
                        registSingletonMap.put(name, (RegistSingleton)Class.forName(name).newInstance());
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return registSingletonMap.get(name);
    }

}